export function menuToRoute(menus) {
    const arr = []
    menus.forEach(v => {
        const obj = {}
        if ((v.filePath != null&&v.filePath == '')||v.filePath == null) {
            obj.path = v.url
            obj.name = v.nameZh
            obj.icon = v.icon
        } else {
            obj.path = '/'
            obj.redirect=v.url
        }
        obj.sortCode = v.sortCode
        obj.component = function () {
            return import("../components/layout.vue")
        }
        obj.children = []
        let meta = [{ title: v.nameZh, url: v.url }]
        obj.hidden = !v.isDisable
        if(v.childrens.length>0){
            v.childrens.forEach(cv => {
                obj.children.push(getMenuItem(cv, v.url, meta))
            })
        }else{
            obj.children.push(getMenuItem(v, null, meta))
        }
        arr.push(obj)
    })
    arr.sort((a, b) => b.sortCode - a.sortCode)
    return arr
}
export function initMenu(router, store) {
    if (store.state.menu && store.state.menu.length > 0) {
        return
    } else {
        store.dispatch('getCurrentMenu').then(v => {
            if (v) {
                router.addRoutes(v);
            }
        })
    }
}

function getMenuItem(itemMenu, parentPath, parentMeta) {
    const route = {
        path: null,
        name: null,
        component: null,
        meta: [],
        icon: null,
        hidden: false
    }
    Object.assign(route, itemMenu);
    route.name = itemMenu.nameZh
    route.component = function () {
        return import("../views" + [itemMenu.filePath])
    }
    route.meta.push(parentMeta[0])
    const meta2 = {}
    meta2.url = parentPath
    meta2.title = itemMenu.nameZh
    route.meta.push(meta2)

    route.path = itemMenu.url

    route.hidden = !itemMenu.isDisable
    return route
}